<template>
	<view>
		<view class="warp">
			<view class='toolbar' style="height: auto;overflow-y: auto;">
				<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"
					@tap="format"></view>
				<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"
					@tap="format"></view>
				<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
					data-name="underline" @tap="format"></view>
				<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian"
					data-name="strike" @tap="format"></view>
				<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
					data-name="align" data-value="center" @tap="format"></view>
				<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
					data-name="align" data-value="right" @tap="format"></view>
				<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
					data-name="align" data-value="justify" @tap="format"></view>

				<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>

				<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
					data-name="color" data-value="#0000ff" @tap="format"></view>
				<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
					class="iconfont icon-fontbgcolor" data-name="backgroundColor" data-value="#00ff00" @tap="format">
				</view>

				<view class="iconfont icon-date" @tap="insertDate"></view>

				<view class="iconfont icon--checklist" data-name="list" data-value="check" @tap="format"></view>
				<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
					data-name="list" data-value="ordered" @tap="format"></view>
				<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
					data-name="list" data-value="bullet" @tap="format"></view>

				<view class="iconfont icon-undo" @tap="undo"></view>
				<view class="iconfont icon-redo" @tap="redo"></view>

				<view class="iconfont icon-outdent" data-name="indent" data-value="-1" @tap="format"></view>
				<view class="iconfont icon-indent" data-name="indent" data-value="+1" @tap="format"></view>
				<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
				<view class="iconfont icon-charutupian" @tap="insertImage"></view>
				<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
					data-name="header" :data-value="1" @tap="format"></view>
			</view>
			<view class="content" style="margin-top: 20px;">
				<editor id="editor" class="ql-container" :placeholder="placeholder" @ready="onEditorReady"  @input="onChange"></editor>
			</view>
		</view>
	</view>

</template>

<script>
	import config from '../../utils/config.js'
	export default {
		name: '',
		props: {
			initContent: {
				default: ""
			},
		},
		data() {
			return {
				formats: {},
				placeholder: '请输入内容...',
				editorCtx: {},
				show_modal: false,
				content: '',
			}
		},
		watch:{
			initContent:function(){
				if(this.initContent!=""){
					let EContent = {
						html: this.initContent
					}
					this.editorCtx.setContents(EContent);
				}
			},
		},
		mounted() {
		},
		methods: {
			onChange(e) {
				this.$emit('updateEditorContent', e.detail.html);
			},
			onEditorReady() {
				uni.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context
					let EContent = {
						html: this.content
					}
					this.editorCtx.setContents(EContent);
				}).exec()
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				this.editorCtx.format(name, value)
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				let _this = this;
				uni.chooseImage({
					count: 1, // 最多只能选择一张图片
					success(res) {
						let file = res.tempFiles[0]; // 获取临时路径
						if (file.size > 3145728) {
							uni.showToast({
								title: "上传图片大小不能超过15mb",
								icon: "none"
							});
							return
						}
						uni.showLoading({
							title: '正在上传...'
						}); // 显示加载提示
						let url = config.domain + 'manage/generic/pic/upload';
						uni.uploadFile({
							header: {
								"token": _this.$store.state.token
							},
							name: "file",
							filePath: file.path,
							url: url, // 服务器接口地址
							success(res) {
								uni.hideLoading(); // 隐藏加载提示
								let resData = JSON.parse(res.data);
								if (resData.code == 0) {
									uni.showToast({
										title: "上传成功",
										icon: "none"
									});
									let imgurl = resData.data;
									_this.editorCtx.insertImage({
										src: imgurl,
										alt: '图像',
										success: function() {
											console.log("success")
										}
									})
								} else {
									uni.showToast({
										title: "上传失败",
										icon: "none"
									});
								}
							},
							fail(err) {
								uni.hideLoading(); // 隐藏加载提示
								uni.showToast({
									title: "上传失败",
									icon: "none"
								});
							}
						})
					}
				})
			},
		}
	}
</script>

<style scoped>
	@import "./editor-icon.css";

	.warp {
		width: 100%;
		height: 100%;
		background-color: white;
		padding: 20px;
		box-sizing: border-box;
	}

	.iconfont {
		width: 32px;
		height: 32px;
		cursor: pointer;
		font-size: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
		margin-bottom: 10px;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-items: center;
		border: 1px solid #000;
		margin-top: 20px;
	}

	.ql-active {
		color: #06c;
	}

	.content {
		height: 62%;
		overflow-y: scroll;
	}

	.ql-container {
		min-height: 400px;
	}
</style>